import Vue from 'vue'
import VueRouter from 'vue-router'
import adminRoutes from './admin'
import store from "../store"
Vue.use(VueRouter)

var routes = [...adminRoutes];

const router = new VueRouter({
  routes
})
//1.已经登陆，访问登陆页面，跳转到页面
//2.已经登陆：访问其他界面，直接放行
//3.未登录状态，访问登陆页面，直接放行
//4.未登录状态，访问其他页面，跳转到登录
// import store from '../store';

router.beforeEach((to, from, next) => {
  if (store.getters['login/getIsLogin']) {
    if (to.path == '/admin/login') {
      next('/admin')
    } else {
      next()
    }
  } else {
    if (to.path == '/admin/login') {
      next()
    } else {
      next('/admin/login')
    }
  }
})
//离开编辑页面的时候  清除vuex的数据
router.afterEach(function (to, from) {
  if (from.path == '/admin/artical/addartical') {
    store._mutations['artical/removeArticalObj'][0]()
  }
})

export default router
